/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/14
 * Time: 03:52
 */
$(function(){
    var ue = UE.getEditor('editor', {initialFrameHeight: 250});
    sendAjax('/v1/regions.json', 'get', {}, function(json) {
        var data = json.body.rows;
        if (data.length > 0) {
            $.each(data, function(n, v) {
                var option = '<option value="'+ v.id+'">'+ v.name+'</option>';
                $('.apart-region').append(option);
            });
        }
        sendAjax('/v1/standards.json', 'get', {}, function(json) {
            var data = json.body.rows;
            if (data.length > 0) {
                $.each(data, function(n, v) {
                    var option = '<option value="'+ v.id+'">'+ v.name+'</option>';
                    $('.apart-standard').append(option);
                });
            }
            sendAjax('/v1/manager/apart/detail.json', 'get', {id:id}, function(json){
                var data = json.body.data;
                if (data) {
                    $('.apart-name').val(data.name);
                    $('.apart-region').val(data.region);
                    $('.apart-region option').each(function() {
                        if ($(this).val() == data.region) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $('.apart-type').val(data.type);
                    $('.apart-type option').each(function(){
                        if ($(this).val() == data.type) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $('.apart-address').val(data.address);
                    $('.apart-area').val(data.area);
                    $('.apart-area option').each(function(){
                        if ($(this).val() == data.area) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $('.apart-standard').val(data.standard);
                    $('.apart-standard option').each(function() {
                        if ($(this).val() == data.standard) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $('.apart-orientations').val(data.orientations);
                    $('.apart-orientations option').each(function() {
                        if ($(this).val() == data.orientations) {
                            $(this).attr('selected', 'selected');
                        }
                    });
                    $('.apart-rent').val(data.rent);
                    $('.apart-other').val(data.other);
                    ue.ready(function(){
                        this.setContent(data.other);
                    });
                    $('.apart-other').val(data.other);
                    $('input[name="apart-status"]').each(function() {
                        if ($(this).val() == data.status) {
                            $(this).attr('checked', 'checked');
                        }
                    });
                }
            });
        });
    });

    $('.btn-save').on('click', function() {
        $('[name="other"]').val(ue.getContent());
        var data = {
            id : $('.apart-id').val(),
            name : $('.apart-name').val(),
            region : $('.apart-region').val(),
            type : $('.apart-type').val(),
            address : $('.apart-address').val(),
            area : $('.apart-area').val(),
            standard : $('.apart-standard').val(),
            orientations : $('.apart-orientations').val(),
            rent : $('.apart-rent').val(),
            other : $('.apart-other').val(),
            status : $('.apart-status').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/update/apart', 'post', {data:data}, function(json) {
                var div = '<div><input type="hidden" class="jump-url" value="/manager/apart.html">' + json.message + '</div>';
                $('#action-tip .modal-body .tip-content').html(div);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        if ($(this).find('.jump-url').val()) {
            location.href = $(this).find('.jump-url').val();
        }
        $(this).find('.tip-content').text('');
    });
});

function checkForm(data) {
    if (!data.id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.name) {
        $('#action-tip .modal-body .tip-content').text('请填写房源名称');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.region) {
        $('#action-tip .modal-body .tip-content').text('请选择所在地');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.type) {
        $('#action-tip .modal-body .tip-content').text('请选择类型');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.address) {
        $('#action-tip .modal-body .tip-content').text('请填写地址');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.area) {
        $('#action-tip .modal-body .tip-content').text('请选择面积');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.standard) {
        $('#action-tip .modal-body .tip-content').text('请选择户型');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.orientations) {
        $('#action-tip .modal-body .tip-content').text('请选择朝向');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.rent) {
        $('#action-tip .modal-body .tip-content').text('请填写月租金');
        $('#action-tip').modal('show');
        return false;
    }

    return true;
}